package com.guo.hui.service;

import org.apache.cxf.interceptor.Interceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.guo.hui.util.PropertiesUtil;

public class ProxyFactory {

	// 查询申请单在TMS系统的状态
	public static <T> T getMobile(Class<T> c) {
		String address = getAddress("Mobile_Url");
//		OutMessageInterceptor i = new OutMessageInterceptor();
		return getPort(c, address, null);
	}

	public static String getAddress(String propertyName) {
		PropertiesUtil util = new PropertiesUtil("cxf.properties");
		String address = util.getValue(propertyName);
		return address;
	}

	@SuppressWarnings("unused")
	private static String getAddress(String path, String propertyName) {
		PropertiesUtil util = new PropertiesUtil(path);
		String address = util.getValue(propertyName);
		return address;
	}

	// 获取WebService接口
	public static <T> T getPort(Class<T> c, String address) {
		return (T) getPort(c, address, null);

	}

	@SuppressWarnings("unchecked")
	private static <T> T getPort(Class<T> c, String address, Interceptor<?> interceptor) {
		JaxWsProxyFactoryBean soapFactoryBean = new JaxWsProxyFactoryBean();
		soapFactoryBean.setAddress(address);
		soapFactoryBean.setServiceClass(c);
		if (interceptor != null) {
			if (interceptor instanceof OutMessageInterceptor) {
				soapFactoryBean.getOutInterceptors().add(interceptor);
			}

			if (interceptor instanceof InMessageInterceptor) {
				soapFactoryBean.getInInterceptors().add(interceptor);
			}
		}
		return (T) soapFactoryBean.create();

	}

}
